package com.soujiayi.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Toast;

import com.soujiayi.model.UpdateInfo;
import com.soujiayi.net.NetworkUtils;
import com.soujiayi.util.Constants;

public class IndexActivity extends Activity {

	private long exitTime = 0;
	private UpdateManager updateMan;
	private ProgressDialog updateProgressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		setContentView(R.layout.home);
		
		if (NetworkUtils.isNetworkConnected(this)) {
			if (checkSessionId()
					&& !((MyApplication) this.getApplication()).isUpdateShown()) {
				updateMan = new UpdateManager(this, appUpdateCb);
				updateMan.checkUpdate();
				((MyApplication) this.getApplication()).setUpdateShown(true);
			}
		}
	}

	public boolean checkSessionId() {
		if ("".equals(((MyApplication) this.getApplication()).getSessionId())
				|| ((MyApplication) this.getApplication()).getSessionId() == null) {
			Toast.makeText(getApplicationContext(),
					this.getString(R.string.invalid_http_result),
					Toast.LENGTH_SHORT).show();
			return false;
		} else
			return true;
	}

	public void search(View view) {
		Intent intent = new Intent(IndexActivity.this, SearchActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
	}

	public void classifySearch(View view) {
		Intent intent = new Intent(IndexActivity.this, SearchActivity.class);
		intent.putExtra(Constants.PRODUCT_IS_CLASSIFY_SHOWED, true);
		startActivity(intent);
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
	}

	public void tuangou(View view) {
		Intent intent = new Intent(IndexActivity.this, TuanMainActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
	}

	public void coupon(View view) {
		Intent intent = new Intent(IndexActivity.this, CouponActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
	}

	public void settings(View view) {
		Intent intent = new Intent(IndexActivity.this, SettingActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
	}

	@Override
	public void onBackPressed() {
		if ((System.currentTimeMillis() - exitTime) > 2000) {
			Toast.makeText(IndexActivity.this,
					this.getString(R.string.click2exit), Toast.LENGTH_SHORT)
					.show();
			exitTime = System.currentTimeMillis();
		} else {
			 ((MyApplication) this.getApplication()).exit();
		}
		if (updateProgressDialog != null && updateProgressDialog.isShowing()) {
			updateMan.cancelDownload();
			updateProgressDialog.dismiss();
		}
	}

	// 自动更新回调函数
	UpdateManager.UpdateCallback appUpdateCb = new UpdateManager.UpdateCallback() {

		public void downloadProgressChanged(int progress) {
			if (updateProgressDialog != null
					&& updateProgressDialog.isShowing()) {
				updateProgressDialog.setProgress(progress);
			}

		}

		public void downloadCompleted(Boolean success, CharSequence errorMsg) {
			if (updateProgressDialog != null
					&& updateProgressDialog.isShowing()) {
				updateProgressDialog.dismiss();
			}
			if (success) {
				updateMan.update();
			} else {
				AlertDialog.Builder builder = new Builder(IndexActivity.this);
				builder.setTitle(R.string.soft_reupdate_title);
				builder.setMessage(R.string.soft_update_failed_info);
				// 更新
				builder.setPositiveButton(R.string.soft_reupdate,
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								// 显示下载对话框
								updateProgressDialog = new ProgressDialog(
										IndexActivity.this);
								updateProgressDialog
										.setMessage(getText(R.string.dialog_downloading_msg));
								updateProgressDialog.setIndeterminate(false);
								updateProgressDialog
										.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
								updateProgressDialog.setMax(100);
								updateProgressDialog.setProgress(0);
								updateProgressDialog.show();

								updateMan.downloadPackage();

							}
						});
				// 稍后更新
				builder.setNegativeButton(R.string.soft_update_later,
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
				Dialog noticeDialog = builder.create();
				noticeDialog.show();
			}
		}

		public void downloadCanceled() {
			// TODO Auto-generated method stub
			if (updateProgressDialog != null
					&& updateProgressDialog.isShowing())
				updateProgressDialog.dismiss();
		}

		public void checkUpdateCompleted(Boolean hasUpdate,
				UpdateInfo updateInfo) {
			if (hasUpdate) {
				// 构造对话框
				AlertDialog.Builder builder = new Builder(IndexActivity.this);
				builder.setTitle(R.string.soft_update_title);
				builder.setMessage(IndexActivity.this
						.getString(R.string.newVersion)
						+ updateInfo.getVerName()
						+ "\n"
						+ IndexActivity.this.getString(R.string.appSize)
						+ updateInfo.getSize()
						+ "\n"
						+ IndexActivity.this.getString(R.string.updateLog)
						+ "\n" + updateInfo.getDesc());
				// 更新
				builder.setPositiveButton(R.string.soft_update_now,
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								// 显示下载对话框
								updateProgressDialog = new ProgressDialog(
										IndexActivity.this);
								updateProgressDialog
										.setMessage(getText(R.string.dialog_downloading_msg));
								updateProgressDialog.setIndeterminate(false);
								updateProgressDialog
										.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
								updateProgressDialog.setMax(100);
								updateProgressDialog.setProgress(0);
								updateProgressDialog.show();

								updateMan.downloadPackage();
							}
						});
				// 稍后更新
				builder.setNegativeButton(R.string.soft_update_later,
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
				Dialog noticeDialog = builder.create();
				noticeDialog.show();
			}
		}
	};
}
